package com.woniu.woniuproject.utils;

import com.woniu.woniuproject.exceptions.SystemMoneyExpection;
import com.woniu.woniuproject.service.ProductAccountService;
import com.woniu.woniuproject.service.moneyCountService;
import com.woniu.woniuproject.service.systemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 功能描述:<br>
 * 〈〉
 * @author EDZ
 * @create 2020/11/17
 * @since 1.0.0
 *
 * debtYearRate  年利率
 * debtMoney  贷款金额
 * month  还款月份
 * Pid  债权id
 * type 交易类型
 */
@Service
public class moneyUtil {
    @Autowired
    ProductAccountService productAccountService;
    @Autowired
    moneyCountService moneyCountService;
    @Autowired
    systemService systemService;

    @Transactional
    public  double changeMoney(double debtYearRate,double debtMoney,int month,int Pid,String type) {
        Double moneycount = moneyCountService.queryall();

            //我们可以赚取的利息
            double moeny = debtMoney * 0.01;
            //每月本金
            double reallymoney = debtMoney;
            for (int i = 1; i <= month; i++) {
                moeny += (debtYearRate / 12) * reallymoney;
                reallymoney = reallymoney - debtMoney / month;
            }
            //5天流标内的公司支出（不考虑5天内有人购买官标产品）
            Map map = productAccountService.queryall();
            List<Integer> list = (List<Integer>) map.get("list");
            List<Double> moneylist = (List<Double>) map.get("money");
            if(moneylist.get(0)==null){
                moneylist.set(0,0.0);
            }
            if(moneylist.get(1)==null){
                moneylist.set(1,0.0);
            }
            if(moneylist.get(2)==null){
                moneylist.set(2,0.0);
            }
            double count = 0;//支出金额
            count += 0.095 / 30 * 5 * moneylist.get(0);
            count += 0.080 / 90 * 5 * moneylist.get(1);
            count += 0.075 / 120 * 5 * moneylist.get(2);
            moeny -= count;
             System.out.println(moeny+"我们可以赚取的利息");
             System.out.println(count+"支出金额");
            if (type.equals("散标")) {
                if (moeny < 0) {//入不敷出
                    return 0;
                } else {
                    if (moeny < debtMoney * 0.08) {//投资回报率小于8%
                        return 0;
                    } else if (moeny < debtMoney * 0.34) {//投资回报率小于34%
                        if (moneyCountService.inmoney(debtMoney * 0.1) > 0 && systemService.sysinsert(Pid, type, debtMoney * 0.1) > 0) {
                            return debtMoney * 0.1;
                        } else {
                            throw new SystemMoneyExpection();
                        }
                    } else {
                        if (moneyCountService.inmoney(debtMoney) > 0 && systemService.sysinsert(Pid, type, debtMoney) > 0) {
                            return debtMoney;
                        } else {
                            throw new SystemMoneyExpection();
                        }
                    }
                }
            } else {
                if (moeny < 0) {//入不敷出
                    return 0;
                } else {
                    if (moeny < debtMoney * 0.08) {//投资回报率小于8%
                        return 0;
                    } else {
                        if (moneyCountService.inmoney(debtMoney) > 0 && systemService.sysinsert(Pid, type, debtMoney) > 0) {
                            return debtMoney;
                        } else {
                            throw new SystemMoneyExpection();
                        }
                    }
                }

            }
        }


}